<?php

class WebAdsmanageController extends Controller {

    public $layout = 'main';

    /**
     * @var返回过滤数组
     * @author nirsh
     * @version 5.0
     * @demo 
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * @var返回验证条件 如果不需要验证权限 把$isPriv 设置为allow
     * @author nirsh
     * @version 5.0
     * @demo 
     *    switch($action){
     *      case "login":
     *          $isPriv = 'allow'
     *          break;
     *      default:
     *          $isPriv = $this->checkAccess($controller, $action);
     *      }
     */
    public function accessRules() {
        $controller = Yii::app()->getController()->id;
        $action = Yii::app()->getController()->action->id;
        //登录不需要判断权限
        $isPriv = strtolower($action)=='adsshow'? 'allow' : $this->checkAccess($controller, $action);
        return array(
            array($isPriv,
                'actions' => array($action),
                'users' => array('*'),
            )
        );
    }

    /**
     * 访问某条具体的记录.
     * @param $id 某条记录的主键
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    /**
     * 创建动作
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionCreate() {
        $model = new WebAdsmanage;
        $model->webset = (int) $_GET['siteid'];
        $varray = WebAdsmanageSort::model()->findAllByAttributes(array('ads_webset' => $model->webset));
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if (isset($_POST['WebAdsmanage'])) {
            $model->attributes = $_POST['WebAdsmanage'];
            if ($model->save()) {
                $url = $this->rewriteCreateUrl('view', array('id' => $model->id, 'WebAdsmanage[sort]' => $_GET["WebAdsmanage"]["sort"], 'siteid' => $_GET[siteid]));
                $this->redirect($url);
            }
        }


        $this->render('create', array(
            'model' => $model,
            'varray' => $varray
        ));
    }

    /**
     * 更新数据 
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionUpdate($id) {
        $model = $this->loadModel($id);
        $varray = WebAdsmanageSort::model()->findAllByAttributes(array('ads_webset' => $model->webset));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if (isset($_POST['WebAdsmanage'])) {
            $model->attributes = $_POST['WebAdsmanage'];
            if ($model->save()) {
                $url = $this->rewriteCreateUrl('view', array('id' => $model->id, 'WebAdsmanage[sort]' => $_GET["WebAdsmanage"]["sort"], 'siteid' => $_GET[siteid]));
                $this->redirect($url);
            }
        }

        $this->render('update', array(
            'model' => $model,
            'varray' => $varray
        ));
    }

    /*
     * 激活AJAX激活状态
     */

    public function actionSetImportant() {
        $id = intval($_REQUEST['id']);
        $model = $this->loadModel($id);
        $model->state = $_REQUEST['state'];
        //$model->resetState($_REQUEST['state']);
        $model->save();
    }

    /**
     * 删除记录 
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionDelete() {
        $sId = $_REQUEST['id'];
        if (is_array($sId)) {
            foreach ($sId as $id) {
                $tModel = $this->loadModel($id);
                $tModel->delete();
            }
        } else {
            $tModel = $this->loadModel($sId);
            $tModel->delete();
        }
        if (isset($_REQUEST['ajax'])) {

            echo "菜单删除成功";
        } else {

            $this->redirect($this->rewriteCreateUrl('admin'));
        }
    }

    /**
     * 列表动作 
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionIndex() {
        $model = new WebAdsmanage('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_REQUEST['WebAdsmanage']))
            $model->attributes = $_REQUEST['WebAdsmanage'];
        $this->render('index', array(
            'model' => $model,
        ));
    }

    /**
     * 数据管理
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionAdmin() {
        $model = new WebAdsmanage('search');

        $model->unsetAttributes();  // clear any default values
        if (isset($_REQUEST['WebAdsmanage']))
            $model->attributes = $_REQUEST['WebAdsmanage'];
        $this->render('admin', array(
            'model' => $model,
        ));
    }

    /**
     * 根据域名获取分类
     */
    public function actionStype() {
        $server = $_REQUEST['server'];
        $sql = "SELECT id,ads_name FROM web_adsmanage_sort WHERE ads_webset=$server";
        $connection = Yii::app()->db;
        $command = $connection->createCommand($sql);
        $result = $command->queryAll();
        echo CJSON::encode($result);
    }

    /**
     * 返回数据模型是基于主键所得到的变量 
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function loadModel($id) {
        $model = WebAdsmanage::model()->findByPk((int) $id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    /**
     * 获取图片代码
     */
    function actionGold() {
        $id = intval($_REQUEST['id']);
        $model = $this->loadModel($id);
        $str = CHtml::link(CHtml::image(Yii::app()->baseUrl . '/' . str_replace(' ', '-', $model->images), $model->title), $model->url);
        echo "document.write('$str');";
    }

    /**
     * ajax验证
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'web-adsmanage-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
